using System;
using csammisrun.OscarLib.Utility;

namespace csammisrun.OscarLib.Utility
{
  /// <summary>
  /// Represents a TLV. TLV stands for Type, Length, Value and is used to transmit data in an organized format
  /// </summary>
  class TLV
  {
    /// <summary>
    /// The type of this TLV
    /// </summary>
    public ushort TypeNumber;
    /// <summary>
    /// The size, in bytes, of the data that follows the header
    /// </summary>
    public ushort DataSize;
    /// <summary>
    /// A byte array of data. Complex data such as strings must be packed into the byte array by the user
    /// </summary>
    public byte[] Data;
  }

	/// <summary>
	/// Summary description for TLV.
	/// </summary>
	internal class TLVMarshal
	{
		public static TLV MakeTLV(ushort type, byte[] data)
		{
			TLV retval = new TLV();
			retval.TypeNumber = type;
			retval.Data = data;
			retval.DataSize = (ushort)((data == null) ? 0 : data.Length);
			return retval;
		}

		public static byte[] TLVsToByteArray(TLV[] tlvs)
		{
			int index = 0;
			return TLVsToByteArray(tlvs, ref index);
		}

		public static byte[] TLVsToByteArray(TLV[] tlvs, ref int index)
		{
			int size = 0;
			foreach(TLV tlv in tlvs)
				size += (tlv.DataSize + 4);
			byte[] retval = new byte[size];
			Marshal.InsertTLVs(tlvs, retval, ref index);
			return retval;
		}
	}
}
